package com.ruoyi.web.controller.base;

import ch.qos.logback.classic.pattern.CallerDataConverter;
import ch.qos.logback.core.pattern.ConverterUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.google.common.base.Joiner;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.MesScanKeysRelation;
import com.ruoyi.system.domain.base.MesSteps;
import com.ruoyi.system.domain.client.MesQualityItems;
import com.ruoyi.system.service.IMesStepsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工步Controller
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@Api(tags = "工步api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/steps" )
public class MesStepsController extends BaseController {

    private final IMesStepsService iMesStepsService;

    /**
     * 查询工步列表
     */
    @ApiOperation(value = "查询工步列表")
    @GetMapping("/list")
    public TableDataInfo list(MesSteps mesSteps)
    {
        startPage();
        LambdaQueryWrapper<MesSteps> lqw = new LambdaQueryWrapper<MesSteps>();
        if (mesSteps.getStepNum() != null){
            lqw.eq(MesSteps::getStepNum ,mesSteps.getStepNum());
        }
        if (StringUtils.isNotBlank(mesSteps.getStepName())){
            lqw.like(MesSteps::getStepName ,mesSteps.getStepName());
        }
        if (StringUtils.isNotBlank(mesSteps.getStepCode())){
            lqw.eq(MesSteps::getStepCode ,mesSteps.getStepCode());
        }
        if (StringUtils.isNotBlank(mesSteps.getRequirement())){
            lqw.eq(MesSteps::getRequirement ,mesSteps.getRequirement());
        }
        if (StringUtils.isNotBlank(mesSteps.getOperationCode())){
            lqw.eq(MesSteps::getOperationCode ,mesSteps.getOperationCode());
        }
        if (mesSteps.getOperationMode() != null){
            lqw.eq(MesSteps::getOperationMode ,mesSteps.getOperationMode());
        }
        if (mesSteps.getBeatTime() != null){
            lqw.eq(MesSteps::getBeatTime ,mesSteps.getBeatTime());
        }
        List<MesSteps> list = iMesStepsService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出工步列表
     */
    @ApiOperation(value = "导出工步列表")
    @Log(title = "工步" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MesSteps mesSteps) {
        LambdaQueryWrapper<MesSteps> lqw = new LambdaQueryWrapper<MesSteps>(mesSteps);
        List<MesSteps> list = iMesStepsService.list(lqw);
        ExcelUtil<MesSteps> util = new ExcelUtil<MesSteps>(MesSteps. class);
        return util.exportExcel(list, "steps" );
    }

    /**
     * 获取工步详细信息
     */
    @ApiOperation(value = "获取工步详细信息")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        MesSteps mesSteps = iMesStepsService.getById(id);
        String relaids = mesSteps.getMesScanKeysRelationIds();
        if(StringUtils.isNotEmpty(relaids)){
            Long [] rels = StringUtils.StringToLong(relaids.split(","));
            mesSteps.setMesScanKeysRelationIdsz(rels);
        }
        String itemids = mesSteps.getMesQualityItemsIds();
        if(StringUtils.isNotEmpty(itemids)){
            Long [] items = StringUtils.StringToLong(itemids.split(","));
            mesSteps.setMesQualityItemsIdsz(items);
        }
        return AjaxResult.success(mesSteps);
    }

    /**
     * 获取扫码绑定-页面采集项
     */
    @ApiOperation(value = "获取扫码绑定-页面采集项")
    @GetMapping(value = "/selectByProductCodeAndStationCode/{operationCode}" )
    public AjaxResult selectByProductCodeAndStationCode(@PathVariable("operationCode" ) String operationCode) {
        List<MesScanKeysRelation> list = iMesStepsService.selectByProductCodeAndStationCode(operationCode);
        return AjaxResult.success(list);
    }

    /**
     * 获取数据下发、数据采集-页面采集项
     */
    @ApiOperation(value = "获取数据下发、数据采集-页面采集项")
    @GetMapping(value = "/selectByStationCode/{operationCode}" )
    public AjaxResult selectByStationCode(@PathVariable("operationCode" ) String operationCode) {
        List<MesQualityItems> list = iMesStepsService.selectByStationCode(operationCode);
        return AjaxResult.success(list);
    }

    /**
     * 新增工步
     */
    @ApiOperation(value = "新增工步")
    @Log(title = "工步" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesSteps mesSteps) {
        Long [] relaids = mesSteps.getMesScanKeysRelationIdsz();
        if(relaids.length > 0){
            String ids = StringUtils.join(relaids, ",");
            mesSteps.setMesScanKeysRelationIds(ids);
        }
        Long [] itemids = mesSteps.getMesQualityItemsIdsz();
        if(itemids.length > 0){
            String ids = StringUtils.join(itemids, ",");
            mesSteps.setMesQualityItemsIds(ids);
        }
        return toAjax(iMesStepsService.save(mesSteps) ? 1 : 0);
    }

    /**
     * 修改工步
     */
    @Log(title = "工步" , businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改工步")
    @PutMapping
    public AjaxResult edit(@RequestBody MesSteps mesSteps) {
        Long [] relaids = mesSteps.getMesScanKeysRelationIdsz();
        if(relaids.length > 0){
            String ids = StringUtils.join(relaids, ",");
            mesSteps.setMesScanKeysRelationIds(ids);
        }
        Long [] itemids = mesSteps.getMesQualityItemsIdsz();
        if(itemids.length > 0){
            String ids = StringUtils.join(itemids, ",");
            mesSteps.setMesQualityItemsIds(ids);
        }
        return toAjax(iMesStepsService.updateById(mesSteps) ? 1 : 0);
    }

    /**
     * 删除工步
     */
    @Log(title = "工步" , businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除工步")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMesStepsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    public static void main(String[] args) {
//        Long [] aa = {1L,2L,3L};
//        String ids = StringUtils.join(aa, ",");
//        String ii = Joiner.on(',').join(aa);
//        System.out.println(ids.toString());
//        System.out.println(ii.toString());
        String [] str = {"1","2","3"};
        Long [] ss = StringUtils.StringToLong(str);
        for (int i = 0; i < ss.length; i++) {
            System.out.println(ss[i]);
        }
    }
}
